<template>
  <div class="mod-menu">
    <el-form :inline="true" size="small" :model="dataForm">
      <el-form-item>
        <el-input
          v-model="dataForm.name"
          placeholder="菜单名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="query()">查询</el-button>
        <!-- <el-button v-if="isAuth('sys:menu:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      size="mini"
      row-key="id"
      border
      ref="menuTreeTable"
      style="width: 100%"
      :expand-row-keys="selectNode"
    >
      <el-table-column type="index" align="center" label="编号" width="50" />
      <el-table-column
        prop="name"
        header-align="center"
        min-width="150"
        align="left"
        label="名称"
      >
      </el-table-column>
      <el-table-column
        prop="parentName"
        header-align="center"
        align="center"
        width="120"
        label="上级菜单"
      >
      </el-table-column>
      <el-table-column header-align="center" align="center" label="图标">
        <template slot-scope="scope">
          <icon-svg :name="scope.row.icon || ''"></icon-svg>
        </template>
      </el-table-column>
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="类型"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 0" size="small">目录</el-tag>
          <el-tag v-else-if="scope.row.type === 1" size="small" type="success"
            >菜单</el-tag
          >
          <el-tag v-else-if="scope.row.type === 2" size="small" type="info"
            >按钮</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="orderNum"
        header-align="center"
        align="center"
        label="排序号"
      >
      </el-table-column>
      <el-table-column
        prop="url"
        header-align="center"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
        label="菜单URL"
      >
      </el-table-column>
      <el-table-column
        prop="perms"
        header-align="center"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
        label="授权标识"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="200"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.type !== 2"
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row, 1)"
            >添加下级</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row, 2)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./menu-add-or-update";
import { treeDataTranslate } from "@/utils";
import { pageList, remove } from "@/api/menu";

export default {
  data() {
    return {
      dataForm: {
        name: "",
      },
      selectNode: ["2" ,"1"],
      dataList: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
    };
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getDataList();
  },
  methods: {
    query() {
       this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      pageList({ name: this.dataForm.name }).then((data) => {
        if (data && data.code === 0) {
          this.dataList = treeDataTranslate(data.data, "id");
        }
        this.dataListLoading = false;
      });
    },
    // 新增 / 修改
    addOrUpdateHandle(data, addChildren) {
      if (addChildren) {
        this.selectNode = addChildren == 1 ? [data.parentId] : [data.id];
        data.addChildren = addChildren;
      }
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data);
      });
    },
    // 删除
    deleteHandle(row) {
      this.$confirm(`确定进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })

        .then(() => {
          this.selectNode = [row.parentId];
          remove({ id: row.id }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
